<?php
class postController
{
	public function archive($args)
	{
		
	}
	
	public function add($args)
	{
		if($_POST)
		{
			// validate form
			if(PFBC\Form::isValid("post_add", false))
			{
				$valid = true;
				
				/*
				if(false)
				{
					$valid = false;
					PFBC\Form::setError("post_add", "Foutmelding 1.", "custom_error_1");
				}
				*
				*/
				
				if(!$valid)
				{
					header("Location: " . $_SERVER["REQUEST_URI"]);
				}
				else
				{
					$article = new Articles();
					$article->setMemberId(Authentication::getCurrentUser()->get_id());
					$article->setTitle($_POST['title']);
					$article->setDateCreated(new DateTime());
					$article->save();
					
					global $driver;
					global $dbinfo;
					global $params;
					$tr =& Translation2_Admin::factory($driver, $dbinfo, $params);
					$languages = $tr->getlangs('array');
					
					$translations = array();
					
					foreach($languages as $language)
					{
						$translations[$language["id"]] = $_POST['content_' . $language["id"]];
					}
					
					$tr->add(urlencode($article->getTitle()), $article->getId(), $translations);
					
					PFBC\Form::clearValues("post_add");
					PFBC\Form::clearErrors("post_add");
					
					$smarty = new BlogSmarty();
					$smarty->assign("message", "Post succesvol geplaatst.");
					$smarty->assign("url", "/");
					$smarty->display("success.tpl");
				}
			}
			else
			{
				header("Location: " . $_SERVER["REQUEST_URI"]);
			}
		}
		else
		{
			$smarty = new BlogSmarty();
			
			// generate form
			$form = new PFBC\Form("post_add", "400");
			$form->addElement(new PFBC\Element\Textbox("Titel", "title", array(
				"validation" => array(
	                new PFBC\Validation\Required
				)
			)));
			
			global $driver;
			global $dbinfo;
			global $params;
			$tr =& Translation2::factory($driver, $dbinfo, $params);
			$languages = $tr->getlangs('array');
			foreach($languages as $language)
			{
				$form->addElement(new PFBC\Element\CKEditor("Inhoud " . $language["name"], "content_" . $language["id"], array(
					"validation" => array(
		                new PFBC\Validation\Required
		            )
				)));
			}
			
			$form->addElement(new PFBC\Element\Button("Post"));
			
			$smarty->assign("form", $form->render(true));
			
			$smarty->assign("title", "Post toevoegen");
			
			$smarty->display("form.tpl");
			
			PFBC\Form::clearValues("post_add");
			PFBC\Form::clearErrors("post_add");
		}
	}
	
	public function read($args)
	{
		if($_POST)
		{
			// validate form
			if(PFBC\Form::isValid("post_reply", false))
			{
				$reply = new Replies();
				$reply->setArticleId($args[":id"]);
				$reply->setName($_POST["name"]);
				$reply->setEmail($_POST["email"]);
				$reply->setContent($_POST["message"]);
				$reply->setStatus("new");
				$reply->setDateCreated(new DateTime());
				$reply->save();
			}
			PFBC\Form::clearValues("post_reply");
			PFBC\Form::clearErrors("post_reply");
			header("Location: " . $_SERVER["REQUEST_URI"]);
		}
		else
		{
			$post = ArticlesQuery::create()->findPK($args[":id"]);
			if(!$post) {
				throw new ModelDoesNotExistException("Article does not exist.");
			}
			
			$smarty = new BlogSmarty();
			
			global $driver;
			global $dbinfo;
			global $params;
			$tr =& Translation2::factory($driver, $dbinfo, $params);
			$tr->setLang(Language::get());
			$translations = $tr->getPage($post->getId());
			
			$smarty->assign("title", $post->getTitle());
			$date_created = new DateTime($post->getDateCreated());
			$smarty->assign("created_on", $date_created->format('H:i:s, d-m-Y'));
			if($post->getDateModified()) {
				$date_updated = new DateTime($post->getDateModified());
				$smarty->assign("updated_on", $date_updated->format('H:i:s, d-m-Y'));
			}
			$smarty->assign("content", array_pop($translations));
			
			//replies
			$replies = RepliesQuery::create()->filterByArticleId($post->getId())->orderByDateCreated()->find();
			foreach($replies as $reply) {
				$date_created = new DateTime($reply->getDateCreated());
				$created_on = $date_created->format('H:i:s, d-m-Y');
				$name = $reply->getName();
				if (!$name)
				{
					$name = "Anonymous";
				}
				$smarty_replies[] = array("id" => $reply->getId(), "content" => $reply->getContent(), "created_on" => $created_on, "name" => $name);
			}
			if ($smarty_replies)
			{
				$smarty->assign("replies", $smarty_replies);
			}
			
			$form = new PFBC\Form("post_reply", "540");
			$form->addElement(new PFBC\Element\Textbox("Name", "name"));
			$form->addElement(new PFBC\Element\Email("Email address", "email", array(
				"validation" => array(
					new PFBC\Validation\Email
				)
			)));
			$form->addElement(new PFBC\Element\Textarea("Message", "message", array(
				"validation" => array(
	                new PFBC\Validation\Required
				)
			)));
			$form->addElement(new PFBC\Element\Button("Reply"));
			
			$smarty->assign("form", $form->render(true));
			$smarty->display("read.tpl");
			
			PFBC\Form::clearValues("post_reply");
			PFBC\Form::clearErrors("post_reply");
		}
	}
	
	public function remove($args)
	{
		$post = ArticlesQuery::create()->findPK($args[":id"]);
		if(!$post) {
			throw new ModelDoesNotExistException("Article does not exist.");
		}
		
		$post->delete();
		
		header("Location: /admin");
	}
	
	public function update($args)
	{
		$post = ArticlesQuery::create()->findPK($args[":id"]);
		if(!$post) {
			throw new ModelDoesNotExistException("Article does not exist.");
		}
		
		if($_POST)
		{
			// validate form
			if(PFBC\Form::isValid("post_update", false))
			{
				$valid = true;
				
				/*
				if(false)
				{
					$valid = false;
					PFBC\Form::setError("post_add", "Foutmelding 1.", "custom_error_1");
				}
				*
				*/
				
				if(!$valid)
				{
					header("Location: " . $_SERVER["REQUEST_URI"]);
				}
				else
				{
					$post->setTitle($_POST["title"]);
					$post->setDateModified(new DateTime());
					$post->save();
					
					global $driver;
					global $dbinfo;
					global $params;
					$tr =& Translation2_Admin::factory($driver, $dbinfo, $params);
					$languages = $tr->getlangs('array');
					$translations = array();
					
					foreach($languages as $language)
					{
						$translations[$language["meta"]] = $_POST['content_' . $language["meta"]];
					}
					$string = TextsQuery::create()->filterByPageId($post->getId())->findOne();
					$tr->update($string->getId(), $post->getId(), $translations);
					
					PFBC\Form::clearValues("post_update");
					PFBC\Form::clearErrors("post_update");
					
					$smarty = new BlogSmarty();
					$smarty->assign("message", "Post succesvol bijgewerkt.");
					$smarty->assign("url", "/");
					$smarty->display("success.tpl");
				}
			}
			else
			{
				header("Location: " . $_SERVER["REQUEST_URI"]);
			}
		}
		else
		{
			$smarty = new BlogSmarty();
			
			// generate form
			$form = new PFBC\Form("post_update", "400");
			$form->addElement(new PFBC\Element\Textbox("Titel", "title", array(
				"validation" => array(
	                new PFBC\Validation\Required
				),
		        "value" => $post->getTitle()
			)));
			
			global $driver;
			global $dbinfo;
			global $params;
			$tr =& Translation2::factory($driver, $dbinfo, $params);
			$languages = $tr->getlangs('array');
			foreach($languages as $language)
			{
				$tr->setLang($language["id"]);
				$translations = $tr->getPage($post->getId());
				
				$form->addElement(new PFBC\Element\CKEditor("Inhoud " . $language["name"], "content_" . $language["id"], array(
					"validation" => array(
		                new PFBC\Validation\Required
		            ),
		            "value" => array_pop($translations)
				)));
			}
			
			$form->addElement(new PFBC\Element\Button("Bewerk"));
			
			$smarty->assign("form", $form->render(true));
			
			$smarty->assign("title", "Post bewerken");
			
			$smarty->display("form.tpl");
			
			PFBC\Form::clearValues("post_update");
			PFBC\Form::clearErrors("post_update");
		}
	}
}
?>